package com.mls.fivedistrict.server.impl;

import com.mls.fivedistrict.common.GDResponse;
import com.mls.fivedistrict.common.RedisPool;
import com.mls.fivedistrict.server.PublickService;
import com.mls.fivedistrict.util.JsonUtils;
import com.mls.fivedistrict.util.jedis.RedisPoolUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

/**
 * Created by chenxiuxiang on 2018/4/2.
 */
@Service("publickService")
public class PublickServiceImpl implements PublickService {
    @Override
    public String getPublick() {
        String publick = RedisPoolUtil.get("publick");
        if (StringUtils.isEmpty(publick)) {
            //获取国盾公钥
            RestTemplate restTemplate = new RestTemplate();
            String forObject = restTemplate.getForObject("https://www.guodunc.com/api/Five/get_public_rsa_string", String.class);
            GDResponse response = JsonUtils.jsonToPojo(forObject, GDResponse.class);
            if (response.getStatus() != 0) {
                RedisPoolUtil.setEx("publick", response.getData(), 30 * 24 * 60 * 60);
                return response.getData();
            } else {
                return "";
            }
        } else {
            //
            return publick;
        }

    }
}
